package net.hn.hnms.biz.mine.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.hn.hnms.biz.mine.domain.HomeLinkConfig;
import net.hn.hnms.biz.mine.mapper.HomeLinkConfigMapper;
import net.hn.hnms.biz.mine.service.IHomeLinkConfigService;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class HomeLinkConfigServiceImpl extends ServiceImpl<HomeLinkConfigMapper, HomeLinkConfig> implements IHomeLinkConfigService {

    @Override
    public int insert(HomeLinkConfig homeLinkConfig) {
        return baseMapper.insert(homeLinkConfig);
    }

    @Override
    public void deleteBatch(List<String> idList) {
        baseMapper.deleteBatchIds(idList);
    }

    @Override
    public ResponsePage<HomeLinkConfig> selectPageList(HomeLinkConfig homeLinkConfig, PageQuery pageQuery) {
        Page<HomeLinkConfig> page = baseMapper.selectPage(pageQuery.build(), buildQuery(homeLinkConfig).orderByAsc(HomeLinkConfig::getLinkSort)
                .orderByDesc(HomeLinkConfig::getCreateTime));
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public List<HomeLinkConfig> selectList(HomeLinkConfig homeLinkConfig) {
        return baseMapper.selectList(buildQuery(homeLinkConfig).orderByAsc(HomeLinkConfig::getLinkSort)
                .orderByDesc(HomeLinkConfig::getCreateTime));
    }

    /**
     * 构造查询条件
     *
     * @param req
     * @return LambdaQueryWrapper<ExecutionFootageOutput> 查询条件
     */
    private LambdaQueryWrapper<HomeLinkConfig> buildQuery(HomeLinkConfig req) {
        LambdaQueryWrapper<HomeLinkConfig> lqw = new LambdaQueryWrapper<HomeLinkConfig>()
                .like(StrUtil.isNotBlank(req.getLinkName()), HomeLinkConfig::getLinkName, req.getLinkName())
                .like(StrUtil.isNotBlank(req.getLinkAddress()), HomeLinkConfig::getLinkAddress, req.getLinkAddress())
                .and(o -> o.eq(req.getMineCode() != null, HomeLinkConfig::getMineCode, req.getMineCode())
                        .or()
                        .eq(req.getCompanyCode() != null, HomeLinkConfig::getCompanyCode, req.getCompanyCode())
                );
        return lqw;
    }

}
